package pers.luchuan.rest.controller;

import org.springframework.web.bind.annotation.*;
import pers.luchuan.rest.domain.User;

import java.util.Objects;

@RestController
public class UserController {

    @GetMapping("getUser")
    public User getUser(String name) {
        if (Objects.equals(name, "zhangsan")) {

            return new User(18, "张三");
        }
        return null;
    }

    @PostMapping("postUser")
    public User postUser(String name) {
        if (Objects.equals(name, "zhangsan")) {

            return new User(18, "张三");
        }
        return null;
    }

    /**
     * post表单提交会接受到name=zhangsan
     * json提交就是完整的请求体，不会解析，解析出来也是json格式字符串
     * @param name
     * @return
     */
    @PostMapping("postUser1")
    public User postUser1(@RequestBody String name) {
        if (Objects.equals(name, "zhangsan")) {

            return new User(18, "张三");
        }
        return null;
    }

    /**
     * 如果参数没有加@RequestBody注解，默认接受表单提交的参数，实体类也一样，不会接收json提交的参数
     * @param user
     * @return
     */
    @PostMapping("postUser2")
    public User postUser2(User user) {
        if (Objects.equals(user.getName(), "zhangsan")) {

            return new User(18, "张三");
        }
        return null;
    }

    @PostMapping("postUser3")
    public User postUser3(@RequestBody User user) {
        if (Objects.equals(user.getName(), "zhangsan")) {

            return new User(18, "张三");
        }
        return null;
    }

    /**
     * post请求默认会接收?name=zhangsan的参数
     * 也就是说post请求可以同时接受地址栏后的参数和post请求的参数
     * @param name
     * @param user
     * @return
     */
    @PostMapping("postUser4")
    public User postUser4(String name, @RequestBody User user) {
        if (Objects.equals(user.getName(), "zhangsan")) {

            return new User(18, "张三");
        }
        return null;
    }

    @PutMapping("put/{name}")
    public void put(@PathVariable String name) {
        if (Objects.equals(name, "zhangsan")) {

            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }

    }

    @DeleteMapping("delete/{name}")
    public void delete(@PathVariable String name) {
        if (Objects.equals(name, "zhangsan")) {

            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
    }
}
